/**
 * @brief TCP Server Peer.
 *
 * @author Huang RuiGuang
 * @date 2008/12/1
 *
 * Herm library
 * Copyright (c) 2008 by Huang RuiGuang(daemon.huang@gmail.com)
 *
 * Permission to use, copy, modify, distribute and sell this software.
 */

#ifndef HERM_SOCKET_TS_PEER_H
#define HERM_SOCKET_TS_PEER_H

#include "herm/base/network/interface.h"
#include "herm/base/network/accept_handler.h"
#include "herm/base/network/helper.h"

namespace herm{
	class Multiplexor;
	class Address;
	class TimerHeap;
}

class TSPeer : public herm::IPeer, public AcceptHandler
{
public:
	TSPeer();
	~TSPeer();

	void Reset(const herm::Address& addr, herm::Multiplexor* multiplexor, herm::IListener* listener, 
		herm::IMsgHandler* handler, int connTimeout, const herm::TCPeerParam& peerParam);

    int AcceptSessions(bool& busy, herm::int64_t curTick);
	inline TCPSessionObjMgr* GetSessions()
	{
		return m_tcpSessionObjMgr;
	}
	
	void Tick(herm::int64_t curTick);
private:
	virtual herm::ISession* CreateSession(const std::string& servName, 
		const herm::Address& remoteAddr, bool isReconnect,  bool isBlock, 
		herm::INBCNotifier* notifier);
	virtual herm::ISession* GetSession(int idx);
	virtual void DestroySession(int sessinIdx);
	virtual void Finish();

	virtual herm::IListener* GetListener() const;
    virtual herm::IMsgHandler* GetHandler() const { return m_handler; }
private:
	herm::IListener* m_listener;
    herm::IMsgHandler* m_handler;
	TCPSessionObjMgr* m_tcpSessionObjMgr;
	herm::Handle* m_socks;
	herm::TCPeerParam m_peerParam;
	char* m_tmpBuf;
	herm::TimerHeap* m_timerHeap;
	int m_connTimeout;
};

#endif
